package com.cskaoyan.day2._03_homework;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

/**
 * 2，有一批订单信息，订单信息包括 下单人，下单金额，下单时间
 * 1.现在想找出最大的下单金额对应的下单人
 * 2.想找出2022-2023年下单的所有订单信息
 * 3.删除低于平均下单金额的账单
 *
 * @author Common-zhou
 * @since 2023-03-01 19:59
 */
public class Homework1 {
    public static void main(String[] args) throws ParseException {
        // 首先需要生成一部分数据 -- 》 所以可以写一个方法
        Collection<Order> orderData = getOrderData();

        findMaxPrice(orderData);

        //2.想找出2022-2023年下单的所有订单信息
        findBetween2022And2023(orderData);

        // 3.删除低于平均下单金额的账单
        removeLowAvg(orderData);

        // todo 这里有没有什么问题？ orderData现在是什么数据？

    }

    private static void removeLowAvg(Collection<Order> orderData) {

        int count = 0;
        int totalPrice = 0;

        for (Order order : orderData) {
            // 有多少人
            count++;
            // 总分
            totalPrice += order.getOrderPrice();
        }

        double avgPrice = totalPrice * 1.0 / count;

        Iterator<Order> iterator = orderData.iterator();
        while (iterator.hasNext()) {
            Order next = iterator.next();

            if (next.getOrderPrice() < avgPrice) {
                // 当前订单小于平均的。直接删除
                iterator.remove();
            }

        }


    }

    private static void findBetween2022And2023(Collection<Order> orderData) throws ParseException {
        Collection<Order> candidateUser = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date beginDate = simpleDateFormat.parse("2022-01-01");
        Date endDate = simpleDateFormat.parse("2023-12-31");
        for (Order order : orderData) {
            Date orderTime = order.getOrderTime();
            // 如果说时间符合要求  在 2022-01-01 与 2023-12-31之间
            // 则存储这个Order
            if (orderTime.after(beginDate) && orderTime.before(endDate)) {
                candidateUser.add(order);
            }
        }

        System.out.println(candidateUser);
        System.out.println(candidateUser.size());


    }

    private static void findMaxPrice(Collection<Order> orderData) {

        int maxPrice = 0;
        String name = null;

        for (Order order : orderData) {
            if (order.getOrderPrice() > maxPrice) {
                // 如果当前金额大于了最大金额
                // 存储最大金额 和 人名
                maxPrice = order.getOrderPrice();
                name = order.getCustomerName();
            }
        }
        System.out.println(name + "___" + maxPrice);
    }

    private static Collection<Order> getOrderData() throws ParseException {

        Collection<Order> orders = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        orders.add(new Order("zs", 120, simpleDateFormat.parse("2023-01-02")));
        orders.add(new Order("ls", 130, simpleDateFormat.parse("2022-03-02")));
        orders.add(new Order("wu", 150, simpleDateFormat.parse("2022-09-10")));
        orders.add(new Order("zl", 180, simpleDateFormat.parse("2023-08-07")));
        orders.add(new Order("孙悟空", 200, simpleDateFormat.parse("2019-06-02")));
        orders.add(new Order("如来佛祖", 201, simpleDateFormat.parse("2018-05-01")));

        return orders;
    }
}

class Order {
    //下单人，下单金额，下单时间
    String customerName;

    int orderPrice;

    Date orderTime;

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public int getOrderPrice() {
        return orderPrice;
    }

    public void setOrderPrice(int orderPrice) {
        this.orderPrice = orderPrice;
    }

    public Date getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(Date orderTime) {
        this.orderTime = orderTime;
    }

    public Order(String customerName, int orderPrice, Date orderTime) {
        this.customerName = customerName;
        this.orderPrice = orderPrice;
        this.orderTime = orderTime;
    }

    @Override
    public String toString() {
        return "Order{" + "customerName='" + customerName + '\'' + ", orderPrice=" + orderPrice + ", orderTime=" + orderTime + '}';
    }
}

